package gameStates;

import gameStates.absGamesStates.AbsMouseState;
import gameStates.passes.AbsPassManagerState;
import input.Mouse;
import input.Mouse.MouseMode;
import main.listeners.listenerClasses.GameListener;
import main.listeners.listenerClasses.SettingsListener;
import main.listeners.listenerClasses.gui.ChatActivatedEvent;
import main.listeners.listenerClasses.gui.ConsoleToggleEvent;
import main.listeners.listenerClasses.gui.GUIListener;
import main.listeners.listenerClasses.gui.IngameChangedEvent;
import main.listeners.listenerClasses.gui.ScoreBoardToggleEvent;
import main.listeners.listenerClasses.gui.SpawnMenuChangedEvent;

public class MouseState extends AbsMouseState {
	
	private Mouse mouse;
	private MouseMode modeBeforeMenu;
	
	public MouseState(AbsPassManagerState pmg) {
		super("mouse", pmg);
		
		mouse = new Mouse();
		rootNode.attachChild(mouse);
		
		setActive(true);
		
		addListenerToClearer(new GUIListener() {
			@Override
			public void ingameChanged(IngameChangedEvent event) {
				if(!event.isIngame()) {
					modeBeforeMenu = mouse.getLastMode();
					setMouseMode(MouseMode.Menu);
				} else {
					MouseMode newMode = modeBeforeMenu;
					if(newMode == null) {
						if(event.spawnMenuWasActive()) newMode = MouseMode.Menu;
						else newMode = MouseMode.Crosshair;
					}
					setMouseMode(newMode);
				}
			}
			
			@Override
			public void spawnMenuChanged(SpawnMenuChangedEvent event) {
				if(event.getValue()) setMouseMode(MouseMode.Menu);
				else setMouseMode(MouseMode.Crosshair);
			}		
			
			@Override
			public void scoreBoardToggled(ScoreBoardToggleEvent event) {
				if(event.scoreBoardIsActive()) setMouseMode(MouseMode.Menu);
				else setMouseMode(event.spawnMenuWasActive() ? MouseMode.Menu : MouseMode.Crosshair);
			}
			
			@Override
			public void chatActivated(ChatActivatedEvent event) {}
			@Override
			public void consoleToggled(ConsoleToggleEvent event) {}
		});
		
		addListenerToClearer(new SettingsListener() {
			@Override
			public void graphicSettingsChanged() {
				restore();
			}
			
			@Override
			public void commonSettingsChanged() {
				updateCommonSettings();
			}
			
			@Override
			public void controlSettingsChanged() {}
			@Override
			public void soundSettingsChanged() {}
		});
		
		addListenerToClearer(new GameListener() {
			@Override
			public void gameStarted() {
				setMouseMode(MouseMode.Menu);
			}
			
			@Override
			public void gameFinished() {
				setMouseMode(MouseMode.Menu);
			}
			
			@Override
			public void gameDestroyed() {}
		});
	}
	
	@Override
	public void update(float tpf) {
		mouse.updateGeometricState(tpf, true);
		mouse.updateRenderState();
	}
	
	@Override
	public void render(float tpf) {}
	
	@Override
	public void cleanup() {
		super.cleanup();
		removeGameState();
	}
	
	@Override
	public void restore() {
		super.restore();
		rootNode.detachChild(mouse);
		
		MouseMode lastMode = mouse.getLastMode();
		mouse = new Mouse();
		rootNode.attachChild(mouse);
		mouse.setCursor(lastMode);
	}
	
	public void setMouseMode(MouseMode mode) {
		mouse.setCursor(mode);
	}
	
	public void updateCommonSettings() {
		mouse.updateCommonSettings();
	}
}